package com.hx.userserver.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hx.common.vo.Result;
import com.hx.userserver.entity.User;
import com.hx.userserver.service.UserLikeFeignService;
import com.hx.userserver.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/us/api")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "保存用户")
    @PostMapping("/save")
    public Result save(User user){
        user.setCreateTime(new Date());
        boolean save = userService.save(user);
        return save?Result.ok():Result.fail("添加失败");
    }

    @SentinelResource(fallback = "allError") //如果对应的接口出现了故障就会执行降级方法
    @ApiOperation(value = "查询所有用户")
    @GetMapping("/getall")
    public Result getAll(){
        List<User> list = userService.list();
        System.out.println("查询所有用户");
        return Result.ok(list);
    }

    @ApiOperation(value = "修改用户信息")
    @GetMapping("/update")
    public Result update(User user){
        boolean updateById = userService.updateById(user);
        return updateById?Result.ok():Result.fail("修改失败");
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/delete")
    public Result delete(User user){
        boolean removeById=userService.removeById(user);
        return removeById?Result.ok():Result.fail("删除失败");
    }

    @ApiOperation(value = "分页条件查询")
    @GetMapping("/page")
    public Result page(String username, @ApiParam("第几页") Integer page, Integer limit){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(username)){
            wrapper.like(User::getUsername, username);
        }

        Page<User> page1 = userService.page(new Page<>(page, limit), wrapper);
        return Result.ok(page1);
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public boolean login(User user){return userService.login(user);}

    @Autowired
    private UserLikeFeignService userLikeFeignService;

    @ApiOperation(value = "查询用户收藏")
    @GetMapping("/userLike")
    public Result findMusicByUser(Integer userId){
        Object a = userLikeFeignService.findMusicByUser(userId).getData();
        System.out.println(a);
        return userLikeFeignService.findMusicByUser(userId);
    }

    public Result allError(){
        return Result.fail("出现错误了");
    }
}
